#include "threadPool.h"

int commandSelect(char *command, char *temp_path, char *commandPath1, char *root, int netfd, char *fileNameToUser, link_list *path_now_list)
{

    if (memcmp(command, "cd", 2) == 0)
    {
        char temp[4096] = {0};
        char temp_commandPath1[4096] = {0};
        strcpy(temp_commandPath1,commandPath1);
        bool ret1 = address_processing_function(path_now_list,  temp_commandPath1,root);
        printf("real list now----------------------\n");
        //printf_linklist(path_now_list);
        splicing_pipe_state_address(path_now_list,"", temp);
        //printf("temp=%s\n", temp);
        // splicing_pipe_state_address(path_now_list,"", temp_path);//传入传出参数的事故
        //printf("ret2 = %d\n", ret2);
        //printf("temp_temp_path=%s\n", temp_path);
        printf("----------------------\n");
        //cd 完成马上发cd 回客户端
        train_ls_t train;

        bzero(&train, sizeof(train));
        train.length = strlen("cd");
        strcpy(train.data, "cd");
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);

        //cd 完成马上发绝对地址回客户端
        bzero(&train, sizeof(train));
        train.length = strlen(temp);
        strcpy(train.data, temp);
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
        bzero(commandPath1,1024);

        // //cd 命令第一次进来不能使用 ./
    }
    else if (memcmp(command, "ls", 2) == 0)
    {
        train_ls_t train;
        bzero(&train, sizeof(train));
        train.length = strlen("ls");
        strcpy(train.data, "ls");
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
        char message[4096]={0};
        char temp[4096]={0};
        splicing_pipe_state_address(path_now_list, "", temp);

        //把该文件夹的文件全部读到message里，再给用户发送
        readdir_name( temp, message);
        //train_t train;
        bzero(&train, sizeof(train));
        train.length = strlen(message);
        strcpy(train.data, message);
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);

    }

    else if (memcmp(command, "gets", 4) == 0)
    {
        train_t train;
        bzero(&train, sizeof(train));
        train.length = strlen("gets");
        strcpy(train.data, "gets");
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
        char temp[4096]={0};
        splicing_pipe_state_address(path_now_list, "", temp);

        //strcat(temp_path,commandPath1);//先cd 到对应目录才能gets puts
        char open_file_path[4096] ={0};
        char temp_path_copy[500]={0};
        //printf("%s",open_file_path);
       
        sprintf(open_file_path, "%s/%s", temp, commandPath1);
        //printf("open_file_path = %s\n",open_file_path);

        transFile(netfd,open_file_path,commandPath1);
        //transFile(netfd,temp_path,commandPath1);
        // md5(fd);
    }
    else if (memcmp(command, "puts", 4) == 0)
    {
        train_t train;
        bzero(&train, sizeof(train));
        train.length = strlen("puts");
        strcpy(train.data, "puts");
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);

        //发回文件名，并打开文件名开始接收
        bzero(&train, sizeof(train));
        train.length = strlen(commandPath1);
        strcpy(train.data, commandPath1);
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);

        //开始接收
        char temp[4096]={0};
        char open_file_path[4096]={0};
        splicing_pipe_state_address(path_now_list, "", temp);
        sprintf(open_file_path, "%s/%s", temp, commandPath1);
        
        recvFile(netfd,open_file_path); //这里约定 用户只发文件名  客户端加完整路径
        //transFile(netfd, path,fileNameToUser);
        //  md5(fd);
    }
    else if (memcmp(command, "pwd", 3) == 0)
    {
        
        train_t train;
        bzero(&train, sizeof(train));
        train.length = strlen("pwd");
        strcpy(train.data,"pwd");

        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
        bzero(&train, sizeof(train));
        //获取当前地址
        char temp[4096]={0};
        splicing_pipe_state_address(path_now_list,"", temp);
        train.length = strlen(temp);
        strcpy(train.data, temp);
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
    }
    else if (memcmp(command, "remove", 6) == 0)
    {
        train_t train;
        bzero(&train, sizeof(train));
        train.length = strlen("remove");
        strcpy(train.data, "remove");
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
        
        char temp[4096]={0};
        char open_file_path[4096]={0};
        splicing_pipe_state_address(path_now_list, "", temp);
        sprintf(open_file_path, "%s/%s", temp, commandPath1);
        remove_my(open_file_path,netfd);
    }
    else if (memcmp(command, "mkdir", 5) == 0)
    {
        train_t train;
        bzero(&train, sizeof(train));
        train.length = strlen("mkdir");
        strcpy(train.data, "mkdir");
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
    }
    else if (memcmp(command, "close", 5) == 0)
    {
        train_t train;
        bzero(&train, sizeof(train));
        train.length = strlen("close");
        strcpy(train.data, "close");
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
    }
    else
    {
        // 命令不合法
        train_t train;
        bzero(&train, sizeof(train));
        train.length = strlen("do not this command");
        strcpy(train.data, "do not this command");
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
        // 地址不合法
    }
}